var mainAgs, highAgs, middleAgs, lowAgs;

var startCsaDraw = function(url, mainCanvasId, highCanvasId, middleCanvasId,
		lowCanvasId) {
	mainAgs = new CsaArgus(mainCanvasId);
	highAgs = new CsaArgus(highCanvasId);
	middleAgs = new CsaArgus(middleCanvasId);
	lowAgs = new CsaArgus(lowCanvasId);
	var refreshSnapShots = function() {
		$.getJSON(url);
		setTimeout(refreshSnapShots, 1000);
	}
	refreshSnapShots();
}

var data = {};
var dataLength = 300;
var jsonpcallback = function(rst) {
	var msg = "";
	try {
		if (!rst) {
			msg = "return null";
		} else if (rst.success == false) {
			msg += rst.resultMessage;
		} else if (rst.success === true) {
			var snapshots = rst.snapshots;
			for ( var i = 0; i != snapshots.length; i++) {
				var snapshot = snapshots[i];
				name = snapshot.threadPoolName;
				priority = snapshot.threadPriority;
				activeCount = snapshot.activeCount;
				coreSize = snapshot.corePoolSize;
				keepAlive = snapshot.keepAliveSeconds;
				maxSize = snapshot.maxPoolSize;
				poolSize = snapshot.poolSize;
				// msg += "threadPoolName:" + name + ", threadPriority:" +
				// priority + ", activeCount:" + activeCount + ", corePoolSize:"
				// + coreSize + ", keepAliveSeconds:" + keepAlive + ",
				// maxPoolSize:" + maxSize + ", poolSize:" + poolSize + "<br/>";
				if (!data[name]) {
					data[name] = {};
					data[name]['name'] = name;
					data[name]['ymax'] = 0;
					data[name]['data'] = [];
					data[name]['data'][0] = RGraph.array_pad([], dataLength);
					data[name]['data'][1] = RGraph.array_pad([], dataLength);
					data[name]['data'][2] = RGraph.array_pad([], dataLength);
				}
				data[name]['ymax'] = Math.max(data[name]['ymax'], maxSize);
				var curData = data[name]['data'];
				curData[0] = [ maxSize ].concat(curData[0]);
				curData[0].pop();
				curData[1] = [ poolSize ].concat(curData[1]);
				curData[1].pop();
				curData[2] = [ activeCount ].concat(curData[2]);
				curData[2].pop();
			}
			mainAgs.drawLines(data['main']['ymax'] + 10, data['main']['data'],
					[ 'MaxSize', 'PoolSize', 'ActiveCount' ], 'Main');
			highAgs.drawLines(data['High']['ymax'] + 10, data['High']['data'],
					[ 'MaxSize', 'PoolSize', 'ActiveCount' ], 'High');
			middleAgs.drawLines(data['Middle']['ymax'] + 10, data['Middle']['data'],
					[ 'MaxSize', 'PoolSize', 'ActiveCount' ], 'Middle');
			lowAgs.drawLines(data['Low']['ymax'] + 10, data['Low']['data'],
					[ 'MaxSize', 'PoolSize', 'ActiveCount' ], 'Low');
		}
	} catch (e) {
		msg += e;
	}
}

var CsaArgus = function(canvasId) {
	this.canvasId = canvasId;
};

CsaArgus.prototype = {
	log : function(msg) {
		console.log(msg);
	},

	init : function() {
		log('init: ' + this.canvasId);
	},

	drawLines : function(ymax, data, chartkey, title) {
		canvasId = this.canvasId;
		RGraph.Clear(document.getElementById(canvasId));
		//RGraph.ObjectRegistry.Clear();
		var line = new RGraph.Line(canvasId, data);
		line.Set('chart.colors', [ '#f00', '#0f0', '#00f', '#f0f', '#ff0',
				'#0ff' ]);
		line.Set('chart.filled', false);
		line.Set('chart.ymax', ymax);
		line.Set('chart.ylabels.count', 10);
		line.Set('chart.numyticks', 10);
		line.Set('chart.numxticks', 25);
		line.Set('chart.background.grid.autofit.numhlines', 10);
		line.Set('chart.background.grid.autofit.numvlines', 25);
		line.Set('chart.title', title);
		line.Set('chart.title.hpos', 0.05);
		line.Set('chart.title.vpos', 0.65);
		line.Set('chart.key', chartkey);
		line.Set('chart.key.position', 'gutter');
		line.Set('chart.key.position.gutter.boxed', false);
		line.Set('chart.key.interactive', false);
		line.Set('chart.crosshairs', false);
		line.Set('chart.resizable', false);
		line.Set('chart.adjustable', false);
		line.Set('chart.labels', [ 'Now', '1mins', '2mins', '3mins', '4mins',
				'5mins' ]);
		line.Set('chart.stepped', false);
		line.Set('chart.tooltips', data);
		line.Draw();
	}
};